home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / amiga / asrc29k.lha / sockutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  2.5 KB  |  133 lines

  1. #include "global.h"
  2. #include "mbuf.h"
  3. #include "netuser.h"
  4. #include "socket.h"
  5. #include "usock.h"
  6. #include "lapb.h"
  7. #include "tcp.h"
  8. #include "nr4.h"
  9. #include "config.h"
  10.  
  11. /* Convert a socket (address + port) to an ascii string of the form
  12.  * aaa.aaa.aaa.aaa:ppppp
  13.  */
  14. char *
  15. psocket(p)
  16. void *p;    /* Pointer to structure to decode */
  17. {
  18. #if    defined(AX25) || defined(NETROM)
  19.     char tmp[11];
  20. #endif
  21.     static char buf[30];
  22.     union sp sp;
  23.     struct socket socket;
  24.  
  25.     sp.p = p;
  26.     switch(sp.sa->sa_family){
  27.     case AF_INET:
  28.         socket.address = sp.in->sin_addr.s_addr;
  29.         socket.port = sp.in->sin_port;
  30.         strcpy(buf,pinet(&socket));
  31.         break;
  32. #ifdef    AX25
  33.     case AF_AX25:
  34.         pax25(tmp,sp.ax->ax25_addr);
  35.         sprintf(buf,"%s on %s",tmp,sp.ax->iface);
  36.         break;
  37. #endif
  38. #ifdef    NETROM
  39.     case AF_NETROM:
  40.         pax25(tmp,sp.nr->nr_addr.user);
  41.         sprintf(buf,"%s @ ",tmp);
  42.         pax25(tmp,sp.nr->nr_addr.node);
  43.         strcat(buf,tmp);
  44.         break;
  45. #endif
  46.     }
  47.     return buf;
  48. }
  49.  
  50. /* Return ASCII string giving reason for connection closing */
  51. char *
  52. sockerr(s)
  53. int s;    /* Socket index */
  54. {
  55.     register struct usock *up;
  56.  
  57.     if((up = itop(s)) == NULLUSOCK){
  58.         errno = EBADF;
  59.         return Badsocket;
  60.     }
  61.     switch(up->type){
  62. #ifdef    AX25
  63.     case TYPE_AX25I:
  64.         if(up->cb.ax25 != NULLAX25)
  65.             return NULLCHAR;    /* nothing wrong */
  66.         return Axreasons[up->errcodes[0]];
  67. #endif
  68. #ifdef    NETROM
  69.     case TYPE_NETROML4:
  70.         if(up->cb.nr4 != NULLNR4CB)
  71.             return NULLCHAR;    /* nothing wrong */
  72.         return Nr4reasons[up->errcodes[0]];
  73. #endif
  74.     case TYPE_TCP:
  75.         if(up->cb.tcb != NULLTCB)
  76.             return NULLCHAR;    /* nothing wrong */        
  77.         return Tcpreasons[up->errcodes[0]];
  78.     default:
  79.         errno = EOPNOTSUPP;    /* not yet, anyway */
  80.         return NULLCHAR;
  81.     }
  82. }
  83. /* Get state of protocol. Valid only for connection-oriented sockets. */
  84. char *
  85. sockstate(s)
  86. int s;        /* Socket index */
  87. {
  88.     register struct usock *up;
  89.  
  90.     if((up = itop(s)) == NULLUSOCK){
  91.         errno = EBADF;
  92.         return NULLCHAR;
  93.     }
  94.     if(up->cb.p == NULLCHAR){
  95.         errno = ENOTCONN;
  96.         return NULLCHAR;
  97.     }
  98.     switch(up->type){
  99.     case TYPE_TCP:
  100.         return Tcpstates[up->cb.tcb->state];
  101. #ifdef    AX25
  102.     case TYPE_AX25I:
  103.         return Ax25states[up->cb.ax25->state];
  104. #endif
  105. #ifdef    NETROM
  106.     case TYPE_NETROML4:
  107.         return Nr4states[up->cb.nr4->state];
  108. #endif
  109.     default:
  110.         /* Datagram sockets don't have state */
  111.         errno = EOPNOTSUPP;
  112.         return NULLCHAR;
  113.     }
  114.     /*NOTREACHED*/
  115. }
  116. /* Convert a socket index to an internal user socket structure pointer */
  117. struct usock *
  118. itop(s)
  119. register int s;    /* Socket index */
  120. {
  121.     register struct usock *up;
  122.  
  123.     if(s < 0 || s >= Nusock)
  124.         return NULLUSOCK;
  125.  
  126.     up = &Usock[s];
  127.  
  128.     if(up->type == NOTUSED)
  129.         return NULLUSOCK;
  130.     return up;
  131. }
  132.  
  133.